package com.yuju.controller.group;

import java.math.BigDecimal;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.group.enums.AgentCustomReturnCodeEnum;
import com.yuju.group.model.AgentCustomModel;
import com.yuju.group.service.IAgentCustomService;
import com.yuju.util.Utils;
import com.yuju.vo.group.AgentCustomVO;

/**
 * 类名：AgentCustomController  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2017年11月24日 下午1:58:54  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("agentCustom")
public class AgentCustomController extends BaseController<AgentCustomVO, AgentCustomModel> {
	
	@Autowired
	private IAgentCustomService agentCustomService;
	
	@RequestMapping("validateName")
	@ResponseBody
	public Result validateName(@RequestBody JSONObject obj){
		AgentCustomVO agentCustomVO = obj.toJavaObject(AgentCustomVO.class);
		agentCustomVO.setHotelId(null);
		boolean validateName = agentCustomService.validateName(convert(agentCustomVO));
		Result result = new Result();
		if(validateName){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：查询列表<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年11月24日 下午2:14:04 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj){
		AgentCustomVO agentCustomVO = obj.toJavaObject(AgentCustomVO.class);
		agentCustomVO.setHotelId(null);
		Map<String, String> fail = FieldValidator.validateAll(agentCustomVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageList = agentCustomService.pageList(convert(agentCustomVO), agentCustomVO.getPage(), agentCustomVO.getPageSize());
		if(Utils.isNotNull(pageList)){
			result.setPageModel(pageList);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AgentCustomReturnCodeEnum.AGENTCUSTOM_QUERY_SUCCESS.getCode());
			result.setReturnMsg(AgentCustomReturnCodeEnum.AGENTCUSTOM_QUERY_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(AgentCustomReturnCodeEnum.AGENTCUSTOM_QUERY_FAILURE.getCode());
			result.setReturnMsg(AgentCustomReturnCodeEnum.AGENTCUSTOM_QUERY_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：查询单个<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年11月24日 下午2:13:55 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj){
		AgentCustomVO agentCustomVO = obj.toJavaObject(AgentCustomVO.class);
		agentCustomVO.setHotelId(null);
		Map<String, String> fail = FieldValidator.validateAll(agentCustomVO, "get", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		AgentCustomModel find = agentCustomService.find(convert(agentCustomVO));
		if(Utils.isNotNull(find)){
			result.setData(find);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AgentCustomReturnCodeEnum.AGENTCUSTOM_QUERY_SUCCESS.getCode());
			result.setReturnMsg(AgentCustomReturnCodeEnum.AGENTCUSTOM_QUERY_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(AgentCustomReturnCodeEnum.AGENTCUSTOM_QUERY_FAILURE.getCode());
			result.setReturnMsg(AgentCustomReturnCodeEnum.AGENTCUSTOM_QUERY_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：更新<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年11月24日 下午2:11:16 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj){
		AgentCustomVO agentCustomVO = obj.toJavaObject(AgentCustomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(agentCustomVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		if(agentCustomVO.getBillLimit().compareTo(new BigDecimal(0))==1){
			agentCustomVO.setBillBalance(agentCustomVO.getBillLimit());
		}else if(agentCustomVO.getBillLimit().compareTo(new BigDecimal(-1))==0){
			agentCustomVO.setBillBalance(new BigDecimal(0));
		}else{
			agentCustomVO.setBillBalance(null);
		}
		Result result = new Result();
		int i = agentCustomService.updateSelective(convert(agentCustomVO));
		if(i>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AgentCustomReturnCodeEnum.AGENTCUSTOM_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(AgentCustomReturnCodeEnum.AGENTCUSTOM_UPDATE_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(AgentCustomReturnCodeEnum.AGENTCUSTOM_UPDATE_FAILURE.getCode());
			result.setReturnMsg(AgentCustomReturnCodeEnum.AGENTCUSTOM_UPDATE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：删除中介<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年11月24日 下午2:07:07 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj){
		AgentCustomVO agentCustomVO = obj.toJavaObject(AgentCustomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(agentCustomVO, "delete", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int del = agentCustomService.del(agentCustomVO.getId());
		if(del>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AgentCustomReturnCodeEnum.AGENTCUSTOM_DEL_SUCCESS.getCode());
			result.setReturnMsg(AgentCustomReturnCodeEnum.AGENTCUSTOM_DEL_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(AgentCustomReturnCodeEnum.AGENTCUSTOM_DEL_FAILURE.getCode());
			result.setReturnMsg(AgentCustomReturnCodeEnum.AGENTCUSTOM_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：新增中介<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年11月24日 下午2:06:59 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj){
		AgentCustomVO agentCustomVO = obj.toJavaObject(AgentCustomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(agentCustomVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		if(agentCustomVO.getBillLimit().compareTo(new BigDecimal(0))==1){
			agentCustomVO.setBillBalance(agentCustomVO.getBillLimit());
		}else if(agentCustomVO.getBillLimit().compareTo(new BigDecimal(-1))==0){
			agentCustomVO.setBillBalance(new BigDecimal(0));
		}else{
			agentCustomVO.setBillBalance(null);
		}
		Result result = new Result();
		int save = agentCustomService.save(convert(agentCustomVO));
		if(save>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AgentCustomReturnCodeEnum.AGENTCUSTOM_SAVE_SUCCESS.getCode());
			result.setReturnMsg(AgentCustomReturnCodeEnum.AGENTCUSTOM_SAVE_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(AgentCustomReturnCodeEnum.AGENTCUSTOM_SAVE_FAILURE.getCode());
			result.setReturnMsg(AgentCustomReturnCodeEnum.AGENTCUSTOM_SAVE_FAILURE.getDesc());
		}
		return result;
	}
}
